# -*- coding: utf-8 -*-

from __future__ import division
import sys
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QHttp



class CaptchaDialog(QDialog):
    def __init__(self, otsikko, teksti, url, parent=None):
        super(CaptchaDialog, self).__init__(parent)
        self.pic_lbl = QLabel("")
        self.txt_lbl = QLabel("")
        self.lineedit = QLineEdit(
            "")
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignHCenter)
        layout.addWidget(self.pic_lbl)
        hlayout = QHBoxLayout()
        hlayout.addWidget(self.txt_lbl)
        hlayout.addWidget(self.lineedit)
        layout.addLayout(hlayout)
        hlayout2 = QHBoxLayout()
        self.okButton = QPushButton(self)
        self.okButton.setObjectName("okButton")
        hlayout2.addWidget(self.okButton)
        self.cancelButton = QPushButton(self)
        self.cancelButton.setObjectName("cancelButton")
        hlayout2.addWidget(self.cancelButton)
        layout.addLayout(hlayout2)
        
        self.setLayout(layout)
        self.lineedit.setFocus()

        self.connect(self.okButton, SIGNAL("clicked()"), self.accept)
        self.connect(self.cancelButton, SIGNAL("clicked()"), self.reject)
        
        self.okButton.setText(QApplication.translate ("CaptchaDialog", "&OK", None, QApplication.UnicodeUTF8))
        self.cancelButton.setText(QApplication.translate("CaptchaDialog", "&Cancel", None, QApplication.UnicodeUTF8))
        self.status = False
        
        self.setWindowTitle(otsikko)
        self.txt_lbl.setText(teksti)

        self.lataa_animaatio()
#        url = "http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Nuvola_apps_edu_languages.svg/30px-Nuvola_apps_edu_languages.svg.png"
        self.loadImage(url)
#        nam = QNetworkAccessManager()
#        self.connect(nam, SIGNAL("finished()"), self.finishRequest)
#        self.reply = nam.get(QNetworkRequest(QUrl(url)))
        print u"Request lähetetty"

    def lataa_animaatio(self):
        # Load animated GIF
        self.movie = QMovie("resources/latausanimaatio.gif");

        #Make sure the GIF was loaded correctly
        if not self.movie.isValid(): 
            print "Captcha: Animaatiota ei voitu ladata."
            return

        self.pic_lbl.setMovie(self.movie);

    def loadImage(self, imagePath):
        self.movie.start()
        print u"url: " + imagePath
        imgURL = QUrl(imagePath)
        self.m_Http = QHttp( imgURL.host() )
        self.m_TempFile = QTemporaryFile()
        if not self.m_TempFile.open(QIODevice.ReadWrite):
            #error handling
            print "virhe"
            return
        #imgURL.path()
        print u"url2: " + imgURL.path()
        self.m_Http.done.connect(self.slotFinished)
        self.m_Http.get(imagePath, self.m_TempFile)

    def slotFinished(self, error):
        print "Hello!"

        if error: print "Virhe: " + self.m_Http.errorString()
        self.m_Http.close()
        self.m_TempFile.close()
        m_Pixmap = QPixmap( self.m_TempFile.fileName() )
        print "TMPFilename: " + self.m_TempFile.fileName()
        self.movie.stop()
        self.pic_lbl.setPixmap(m_Pixmap)
        self.pic_lbl.show()


   
    def reject(self):
        super(CaptchaDialog, self).reject()
    
    def accept(self):
        super(CaptchaDialog, self).accept()
    
    @staticmethod
    def getText(p,  otsikko,  teksti, url):
        a = CaptchaDialog(otsikko, teksti, url, p)
        a.exec_()
        
        return a.lineedit.text(), a.result()

